using UnityEngine;
using UnityEngine.UI;
public class SelectLevel_UI : MonoBehaviour
{
    public Button startMenu, quit;
    public string[] scene_Level;
    public int[] StarsOfLevel;
    public Button[] Levels;
    public Sprite[] LockedLevel;
    public Sprite[] FaceLevel;

    private void Start()
    {
        OpenStartMenu();
        startMenu.onClick.AddListener(SelectLevel);
        quit.onClick.AddListener(Application.Quit);
        for (int i = 0; i < 5; i++)
        {
            StarsOfLevel[i] = PlayerPrefs.GetInt($"Level_{i}_Rating", 0);

            GameObject target = GameObject.Find($"L{i + 1}Face");
            if (StarsOfLevel[i] != 0)
            {
                target.SetActive(true);
                target.GetComponent<Image>().sprite = FaceLevel[StarsOfLevel[i]];
            }
            else
            {
                target.SetActive(false);
            }
            
            if (i == 0)
            {
                string sceneName = scene_Level[i];
                Levels[i].onClick.AddListener(() => UnityEngine.SceneManagement.SceneManager.LoadScene(sceneName));
                continue;
            }
            if (StarsOfLevel[i - 1] != 0)
            {
                string sceneName = scene_Level[i];
                Levels[i].onClick.AddListener(() => UnityEngine.SceneManagement.SceneManager.LoadScene(sceneName));
            }
            else if (i != 0)
            {
                Levels[i].GetComponent<Image>().sprite = LockedLevel[0];
                Levels[i].spriteState = new SpriteState
                {
                    highlightedSprite = LockedLevel[0],
                    pressedSprite = LockedLevel[1],
                    selectedSprite = LockedLevel[0],
                    disabledSprite = LockedLevel[0]
                };
            }
        }
    }

    private void OpenStartMenu()
    {
        UIController.instance.ClosePage(1);
        UIController.instance.OpenPage(0);
    }

    private void SelectLevel()
    {
        UIController.instance.ClosePage(0);
        UIController.instance.OpenPage(1);        
    }
}
